﻿import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import FilUI

TextField {
    id: control

    property bool disabled                      : false
    property int iconSource                     : 0
    property color normalColor                  : Qt.rgba(27/255, 27/255, 27/255, 1)
    property color disableColor                 : Qt.rgba(160/255, 160/255, 160/255, 1)
    property color placeholderNormalColor       : Qt.rgba(96/255, 96/255, 96/255, 1)
    property color placeholderFocusColor        : Qt.rgba(141/255, 141/255, 141/255, 1)
    property color placeholderDisableColor      : Qt.rgba(160/255, 160/255, 160/255, 1)

    enabled                                     : !disabled
    color                                       : {
        if (!enabled) {
            return disableColor
        }
        return normalColor
    }
    font                                        : FilTextStyle.Body
    padding                                     : 7
    rightPadding                                : 40
    leftPadding                                 : padding + 4
    echoMode                                    : btn_reveal.pressed ? TextField.Normal : TextField.Password
    renderType                                  : Text.QtRendering
    selectionColor                              : FilTools.withOpacity(FilTheme.primaryColor, 0.5)
    selectedTextColor                           : control.color
    placeholderTextColor                        : {
        if (!enabled) {
            return placeholderDisableColor
        }
        if (focus) {
            return placeholderFocusColor
        }
        return placeholderNormalColor
    }
    selectByMouse                               : true
    width                                       : 240
    background                                  : FilTextBoxBackground {
        inputItem                               : control
    }
    Keys.onEnterPressed                         : (event) => d.handleCommit(event)
    Keys.onReturnPressed                        : (event) => d.handleCommit(event)

    signal commit(string text)

    QtObject {
        id: d
        function handleCommit(event) {
            control.commit(control.text)
        }
    }

    FilIconButton {
        id: btn_reveal
        iconSource                              : FilIcons.RevealPasswordMedium
        iconSize                                : 10
        width                                   : 30
        height                                  : 20
        verticalPadding                         : 0
        horizontalPadding                       : 0
        iconColor                               : Qt.rgba(97/255, 97/255, 97/255, 1)
        visible                                 : control.text !== ""
        anchors {
            verticalCenter                      : parent.verticalCenter
            right                               : parent.right
            rightMargin                         : 5
        }
    }
}
